package com.ple.app.importdata;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.ple.app.jzsf.R;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



public class ImportDataActivity extends Activity {


    private TextView textView;
    private Button adddata;
    private Button deletdata;
    private Spinner chooseseedtype;
    private Button export;
    private Button seedbutton;
    private Button manurebutton;
    private int m=0;
    private View view_seed;
    private View view_manure;
    /**
     * 种子信息
     */
    private ListView listView;
    private int operation_id=0;//删除修改是的ID
    private String seed_typevalue;//种子种类
    private ArrayAdapter<String> type_adapter;//种子种类转换适配器
    private String[] conlm=new String[]{"ID","name","other1","other2","wrap"};
    private String[] manure_conlm=new String[]{"ID","name","N","P","K","type"};
   private  static  final String[] seedtype={"所有","玉米","小麦","大豆","水稻" };
    private  static  final String[] manuretype={"所有","钾肥","氮肥","复合肥","磷肥" };
    private  static  final String[] dialog_seedtype={"玉米","小麦","大豆","水稻" };
    private  static  final String[] dialog_manuretype={"钾肥","氮肥","复合肥","磷肥" };
    /**
     *添加信息
     */


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.import_data_tabs);
        /**
         * 种子和肥料信息选择
         */


        textView= (TextView) findViewById(R.id.textview1);
        chooseseedtype= (Spinner) findViewById(R.id.chooseseedtype);
        view_seed=findViewById(R.id.seed_header);
        view_manure=findViewById(R.id.manure_header);

        /**
         * 种类下拉框适配数据
         */
        /**type_adapter=new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,seedtype);
        chooseseedtype.setAdapter(type_adapter);*/

        type_adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,seedtype);
        chooseseedtype.setAdapter(type_adapter);
       initData();//初始化数据


        /**
         * 选择种子或肥料信息展示
         */
        final  ColorStateList colorStateList_blue=getResources().getColorStateList(R.color.colorPrimary);
        final  ColorStateList colorStateList_white=getResources().getColorStateList(R.color.white);
        seedbutton= (Button) findViewById(R.id.seedbutton);
        manurebutton= (Button) findViewById(R.id.manurebutton);
        seedbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Drawable img_on, img_off;
                img_on=getResources().getDrawable(android.R.drawable.star_on);
                img_off=getResources().getDrawable(android.R.drawable.star_off);
                img_off.setBounds(0, 0, img_off.getMinimumWidth(), img_off.getMinimumHeight());
                img_on.setBounds(0, 0, img_off.getMinimumWidth(), img_off.getMinimumHeight());
                seedbutton.setTextColor(colorStateList_blue);
                seedbutton.setCompoundDrawables(img_on,null,null,null);
                manurebutton.setTextColor(colorStateList_white);
                manurebutton.setCompoundDrawables(img_off,null,null,null);
                textView.setText("请选择种子种类：");
                m=0;
                type_adapter=new ArrayAdapter<String>(ImportDataActivity.this,android.R.layout.simple_spinner_item,seedtype);
                chooseseedtype.setAdapter(type_adapter);
                view_manure.setVisibility(View.GONE);
                view_seed.setVisibility(View.VISIBLE);
                seeddatatolistview();
            }
        });
        manurebutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Drawable img_on, img_off;
                img_on=getResources().getDrawable(android.R.drawable.star_on);
                img_off=getResources().getDrawable(android.R.drawable.star_off);
                img_off.setBounds(0, 0, img_off.getMinimumWidth(), img_off.getMinimumHeight());
                img_on.setBounds(0, 0, img_off.getMinimumWidth(), img_off.getMinimumHeight());
                m=1;
                type_adapter=new ArrayAdapter<String>(ImportDataActivity.this,android.R.layout.simple_spinner_item,manuretype);
                chooseseedtype.setAdapter(type_adapter);
                textView.setText("请选择肥料种类：");
                view_manure.setVisibility(View.VISIBLE);
                view_seed.setVisibility(View.GONE);
                seedbutton.setCompoundDrawables(img_off,null,null,null);
                manurebutton.setCompoundDrawables(img_on,null,null,null);
                seedbutton.setTextColor(colorStateList_white);
                manurebutton.setTextColor(colorStateList_blue);
                manuredatatolistview();
            }
        });


        /**
         * 选择种子或肥料种类分类展示
         */
        chooseseedtype.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {


                List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
                if (m==0) {
                    seed_typevalue=seedtype[position];
                    String[] args=new String[]{seed_typevalue};
                    MyopenHelper myopenhelper = new MyopenHelper(ImportDataActivity.this, "seed.db", null, 1);
                    SQLiteDatabase db = myopenhelper.getReadableDatabase();
                    Cursor curor;
                    if (position != 0) {
                         curor = db.query("seed", conlm, "wrap=?", args, null, null, null, null);
                    }//if
                    else{curor = db.query("seed", conlm, null, null, null, null, null, null);}
                        while (curor.moveToNext()) {
                            Map<String, Object> item = new HashMap<String, Object>();
                            String id1 = curor.getString(0);
                            String name = curor.getString(1);
                            String other1 = curor.getString(2);
                            String other2 = curor.getString(3);
                            item.put("ID", id1);
                            item.put("name", name);
                            item.put("other1", other1);
                            item.put("other2", other2);
                            list.add(item);
                        }
                        db.close();curor.close();


                    SimpleAdapter adapter = new SimpleAdapter(ImportDataActivity.this, list, R.layout.seed_item, new String[]{"ID", "name", "other1", "other2"},
                            new int[]{R.id.ID, R.id.Name, R.id.other1, R.id.other2});
                    listView = (ListView) findViewById(R.id.seed_datagrid); //(ListView) findViewById(R.id.seed_datagrid);
                    listView.setAdapter(adapter);
                }//if(m==0)
                else{
                    seed_typevalue=manuretype[position];
                    String[] args=new String[]{seed_typevalue};
                    MyopenHelper myopenhelper = new MyopenHelper(ImportDataActivity.this, "seed.db", null, 1);
                    SQLiteDatabase db = myopenhelper.getReadableDatabase();
                    Cursor curor;
                    if (position != 0) {
                        curor = db.query("manure", manure_conlm, "type=?", args, null, null, null, null);
                    }//if
                    else{curor = db.query("manure", manure_conlm, null, null, null, null, null, null);}
                    while (curor.moveToNext()) {
                        Map<String, Object> item = new HashMap<String, Object>();
                        String sp_mid=curor.getString(0);
                        String sp_mname=curor.getString(1);
                        String n=curor.getString(2);
                        String p=curor.getString(3);
                        String k=curor.getString(4);
                        item.put("ID", sp_mid);
                        item.put("name", sp_mname);
                        item.put("N", n);
                        item.put("P", p);
                        item.put("K", k);
                        list.add(item);}
                    db.close();curor.close();
                    SimpleAdapter adapter = new SimpleAdapter(ImportDataActivity.this, list, R.layout.manure_item, new String[]{"ID", "name", "N", "P","K"},
                            new int[]{R.id.manureID, R.id.manureName, R.id.n, R.id.p,R.id.k});
                    listView = (ListView) findViewById(R.id.seed_datagrid); //(ListView) findViewById(R.id.seed_datagrid);
                    listView.setAdapter(adapter);
                }

            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


        /**
         * 添加数据
         */
        adddata= (Button) findViewById(R.id.add_data);
        adddata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater a=LayoutInflater.from(ImportDataActivity.this);
                final View dialogview=a.inflate(R.layout.add_seedata,null);
                final View manuredialoview=a.inflate(R.layout.add_manure,null);
                final Spinner seed_type= (Spinner) dialogview.findViewById(R.id.seed_type);
                final Spinner manuredialog_type= (Spinner)manuredialoview.findViewById(R.id.manure_type);

                if(m==0){
                    seed_type.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                            //获得下拉框中的值
                            seed_typevalue=dialog_seedtype[position];
                        }
                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {}
                    });//Spinner
                    type_adapter=new ArrayAdapter<String>(ImportDataActivity.this,android.R.layout.simple_spinner_item,dialog_seedtype);
                    seed_type.setAdapter(type_adapter);
                }
                    else{
                    manuredialog_type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                            seed_typevalue=dialog_manuretype[position];
                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {

                        }
                    });
                    type_adapter=new ArrayAdapter<String>(ImportDataActivity.this,android.R.layout.simple_spinner_item,dialog_manuretype);
                    manuredialog_type.setAdapter(type_adapter);}

                final EditText ID=(EditText) dialogview.findViewById(R.id.seed_id);
                final EditText name1=(EditText) dialogview.findViewById(R.id.seed_name);
                final EditText other11=(EditText) dialogview.findViewById(R.id.seed_other1);
                final EditText other12=(EditText) dialogview.findViewById(R.id.seed_other2);

                final EditText manureid=(EditText) manuredialoview.findViewById(R.id.manure_id);
                final EditText manurename=(EditText)manuredialoview.findViewById(R.id.manure_name);
                final EditText n=(EditText)manuredialoview.findViewById(R.id.N);
                final EditText p=(EditText) manuredialoview.findViewById(R.id.P);
                final EditText k=(EditText) manuredialoview.findViewById(R.id.K);

                seed_type.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                         //获得下拉框中的值
                        seed_typevalue=dialog_seedtype[position];
                    }
                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {}
                });//Spinner
                //生成ID
                MyopenHelper myopenhelper1=new MyopenHelper(ImportDataActivity.this,"seed.db",null,1);
                SQLiteDatabase db1=myopenhelper1.getReadableDatabase();
                if(m==0){
                Cursor curor_getid=db1.query("seed",conlm,null,null,null,null,null,null);
                String curor_id ="0";
               if(curor_getid.getCount()!=0&&curor_getid.moveToLast())curor_id=curor_getid.getString(0);
                db1.close();
                    curor_getid.close();
                int id=1+Integer.valueOf(curor_id);
                ID.setText(String.valueOf(id));
                //添加数据
                add(dialogview,ID,name1,other11,other12,null);}
                else{
                    Cursor curor_getid=db1.query("manure",manure_conlm,null,null,null,null,null,null);
                    String curor_id ="0";
                    if(curor_getid.getCount()!=0&&curor_getid.moveToLast())curor_id=curor_getid.getString(0);
                    db1.close();
                    curor_getid.close();
                    int id=1+Integer.valueOf(curor_id);
                    manureid.setText(String.valueOf(id));
                    //添加数据
                    add(manuredialoview,manureid,manurename,n,p,k);}
            }
        });//添加数据结束

        /**
         * 删除数据
         */
        deletdata= (Button) findViewById(R.id.delete_data);
        deletdata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                delet();
            }
        });
        /**
         * 分享数据
         */

        export= (Button) findViewById(R.id.export);
        export.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_SUBJECT, "数据信息");
                intent.putExtra(Intent.EXTRA_TEXT, "");
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(Intent.createChooser(intent, getTitle()));
            }
        });
    }  //oncreat


    /**
     * 下面为自定义函数
     */
    /**
     * 初始化
     */
    private void initData()  {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        MyopenHelper myopenhelper=new MyopenHelper(this,"seed.db",null,1);
        SQLiteDatabase db=myopenhelper.getReadableDatabase();
        Cursor curor=db.query("seed",conlm,null,null,null,null,null,null);
       // Cursor curor1=db.query("manure",manure_conlm,null,null,null,null,null,null);
        //模拟数据
        int i=curor.getCount();
        if (i==0){
            for(int j=1;j<3;j++){
                ContentValues values=new ContentValues();
                ContentValues values1=new ContentValues();
                values.put("ID",j);
                values.put("name","京东"+j+"号");
                values.put("other1",23);
                values.put("other2",95);
                values.put("wrap","小麦");
                db.insert("seed",null,values);

                values1.put("ID",j);
                values1.put("name","氮肥"+j+"号");
                values1.put("N",12);
                values1.put("P",1);
                values1.put("K",2);
                values1.put("type","氮肥");
                db.insert("manure",null,values1);
            }

            curor.close();
            db.close();
        }
        else {
            while(curor.moveToNext()){
                Map<String, Object> item = new HashMap<String, Object>();
                String id=curor.getString(0);
                String name=curor.getString(1);
                String other1=curor.getString(2);
                String other2=curor.getString(3);
                item.put("ID", id);
                item.put("name", name);
                item.put("other1", other1);
                item.put("other2", other2);
                list.add(item);
            }
            curor.close();
            db.close();
        }
        SimpleAdapter adapter = new SimpleAdapter(ImportDataActivity.this, list, R.layout.seed_item, new String[]{"ID", "name", "other1", "other2"},
                new int[]{R.id.ID, R.id.Name, R.id.other1, R.id.other2});
        listView = (ListView) findViewById(R.id.seed_datagrid); //(ListView) findViewById(R.id.seed_datagrid);
        listView.setAdapter(adapter);
        /**
         *  选中状态List view的Item
         */
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                HashMap<String,String> getmap= (HashMap<String, String>) listView.getItemAtPosition(position);
                String ID=getmap.get("ID");
                operation_id=Integer.valueOf(ID);
                //Toast.makeText(getApplicationContext(),ID,Toast.LENGTH_LONG).show();
            }
        });
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(ImportDataActivity.this,InfomationActivity.class);
                startActivity(intent);
                return false;//想长按时不执行单机事件，则设置为true；
            }
        });


    }//initData
    /**
     * 更新种子Listview
     */
    private void seeddatatolistview(){
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        //String[] conlm=new String[]{"ID","name","other1","other2","wrap"};
        MyopenHelper myopenhelper=new MyopenHelper(ImportDataActivity.this,"seed.db",null,1);
        SQLiteDatabase db=myopenhelper.getReadableDatabase();
        Cursor curor=db.query("seed",conlm,null,null,null,null,null,null);
        while(curor.moveToNext()){
            Map<String, Object> item = new HashMap<String, Object>();
            String id=curor.getString(0);
            String name=curor.getString(1);
            String other1=curor.getString(2);
            String other2=curor.getString(3);
            item.put("ID", id);
            item.put("name", name);
            item.put("other1", other1);
            item.put("other2", other2);
            list.add(item);}
        curor.close();
        db.close();
        SimpleAdapter adapter = new SimpleAdapter(ImportDataActivity.this, list, R.layout.seed_item, new String[]{"ID", "name", "other1", "other2"},
                new int[]{R.id.ID, R.id.Name, R.id.other1, R.id.other2});
        listView = (ListView) findViewById(R.id.seed_datagrid); //(ListView) findViewById(R.id.seed_datagrid);
        listView.setAdapter(adapter);

    }
    /**
     * 更新肥料Listview
     */
    private void manuredatatolistview(){
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        MyopenHelper myopenhelper=new MyopenHelper(ImportDataActivity.this,"seed.db",null,1);
       SQLiteDatabase db=myopenhelper.getReadableDatabase();
        Cursor curor=db.query("manure",manure_conlm,null,null,null,null,null,null);
         while(curor.moveToNext()){
            Map<String, Object> item = new HashMap<String, Object>();
            String id=curor.getString(0);
            String name=curor.getString(1);
            String n=curor.getString(2);
            String p=curor.getString(3);
            String k=curor.getString(4);
            item.put("ID", id);
            item.put("name", name);
            item.put("N", n);
            item.put("P", p);
            item.put("K", k);
            list.add(item);}
        curor.close();
        db.close();
        SimpleAdapter adapter = new SimpleAdapter(ImportDataActivity.this, list, R.layout.manure_item, new String[]{"ID", "name", "N", "P","K"},
                new int[]{R.id.manureID, R.id.manureName, R.id.n, R.id.p,R.id.k});
        listView = (ListView) findViewById(R.id.seed_datagrid); //(ListView) findViewById(R.id.seed_datagrid);
        listView.setAdapter(adapter);
    }
    /**
     * 删出数据
     */
    private void delet(){
        if(operation_id==0){
            if(m==0){Toast.makeText(getApplicationContext(),"请选择待删除的种子信息",Toast.LENGTH_LONG).show();}
            else{Toast.makeText(getApplicationContext(),"请选择待删除的肥料信息",Toast.LENGTH_LONG).show();}
        }
        else{
        new AlertDialog.Builder(ImportDataActivity.this).setTitle("系统提示")
                .setMessage("确认删除此条信息？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(m==0){
                        String[] args=new String[]{String.valueOf(operation_id)};
                        MyopenHelper myopenhelper=new MyopenHelper(ImportDataActivity.this,"seed.db",null,1);
                        SQLiteDatabase db=myopenhelper.getReadableDatabase();
                        db.delete("seed","ID=?",args);
                        db.close();
                        seeddatatolistview();
                        Toast.makeText(getApplicationContext(),"已删除种子信息",Toast.LENGTH_LONG).show();}
                        else{
                                String[] args=new String[]{String.valueOf(operation_id)};
                                MyopenHelper myopenhelper=new MyopenHelper(ImportDataActivity.this,"seed.db",null,1);
                                SQLiteDatabase db=myopenhelper.getReadableDatabase();
                                db.delete("manure","ID=?",args);
                                db.close();
                                manuredatatolistview();
                                Toast.makeText(getApplicationContext(),"已删除肥料信息",Toast.LENGTH_LONG).show();}

                    }
                }).setNegativeButton("取消",null).create().show();}
    }
    /**
     * 添加种子信息
     */
    private void add(final View dialogview ,final EditText ID,final EditText name1,final EditText other11,final EditText other12,final EditText k1)
    {

        String title;
        if(m==0){
            title="添加种子信息";
        }else{title="添加肥料信息";}
            new AlertDialog.Builder(ImportDataActivity.this).setTitle(title)
                    .setView(dialogview)
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String name = name1.getText().toString();
                            String other1 = other11.getText().toString();
                            String other2 = other12.getText().toString();

                            if (m == 0) {
                                if (name.equals("") || other1.equals("")) {
                                    Toast.makeText(getApplicationContext(), "种子信息不完整，添加失败！", Toast.LENGTH_LONG).show();
                                } else {
                                    //String[] conlm=new String[]{"ID","name","other1","other2","wrap"};
                                    MyopenHelper myopenhelper = new MyopenHelper(ImportDataActivity.this, "seed.db", null, 1);
                                    SQLiteDatabase db = myopenhelper.getReadableDatabase();
                                    int id = Integer.valueOf(ID.getText().toString());
                                    ContentValues values = new ContentValues();
                                    values.put("ID", id);
                                    values.put("name", name);
                                    values.put("other1", other1);
                                    values.put("other2", other2);
                                    values.put("wrap", seed_typevalue);
                                    db.insert("seed", null, values);
                                    Cursor curor = db.query("seed", conlm, null, null, null, null, null, null);
                                    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
                                    while (curor.moveToNext()) {
                                        Map<String, Object> item = new HashMap<String, Object>();
                                        String id11 = curor.getString(0);
                                        String name11 = curor.getString(1);
                                        String other111 = curor.getString(2);
                                        String other121 = curor.getString(3);
                                        item.put("ID", id11);
                                        item.put("name", name11);
                                        item.put("other1", other111);
                                        item.put("other2", other121);
                                        list.add(item);
                                    }
                                    SimpleAdapter adapter = new SimpleAdapter(ImportDataActivity.this,
                                            list, R.layout.seed_item, new String[]{"ID", "name", "other1", "other2"},
                                            new int[]{R.id.ID, R.id.Name, R.id.other1, R.id.other2});
                                    listView = (ListView) findViewById(R.id.seed_datagrid); //(ListView) findViewById(R.id.seed_datagrid);
                                    listView.setAdapter(adapter);
                                    db.close();
                                    curor.close();
                                }
                            }//ifm==0
                            else {//添加肥料信息
                                if (name.equals("") || other1.equals("")) {
                                    Toast.makeText(getApplicationContext(), "肥料信息不完整，添加失败！", Toast.LENGTH_LONG).show();
                                } else {
                                    String k = k1.getText().toString();
                                    MyopenHelper myopenhelper = new MyopenHelper(ImportDataActivity.this, "seed.db", null, 1);
                                    SQLiteDatabase db = myopenhelper.getReadableDatabase();
                                    int id = Integer.valueOf(ID.getText().toString());
                                    ContentValues values = new ContentValues();
                                    values.put("ID", id);
                                    values.put("name", name);
                                    values.put("N", other1);
                                    values.put("P", other2);
                                    values.put("K", k);
                                    values.put("type", seed_typevalue);
                                    db.insert("manure", null, values);
                                    Cursor curor = db.query("manure", manure_conlm, null, null, null, null, null, null);
                                    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
                                    while (curor.moveToNext()) {
                                        Map<String, Object> item = new HashMap<String, Object>();
                                        String id11 = curor.getString(0);
                                        String name11 = curor.getString(1);
                                        String n11 = curor.getString(2);
                                        String p11 = curor.getString(3);
                                        String k11 = curor.getString(4);
                                        item.put("ID", id11);
                                        item.put("name", name11);
                                        item.put("N", n11);
                                        item.put("P", p11);
                                        item.put("K", k11);
                                        list.add(item);
                                    }
                                    SimpleAdapter adapter = new SimpleAdapter(ImportDataActivity.this, list, R.layout.manure_item, new String[]{"ID", "name", "N", "P", "K"},
                                            new int[]{R.id.manureID, R.id.manureName, R.id.n, R.id.p, R.id.k});
                                    listView = (ListView) findViewById(R.id.seed_datagrid); //(ListView) findViewById(R.id.seed_datagrid);
                                    listView.setAdapter(adapter);
                                    db.close();
                                    curor.close();
                                }//ifm==0
                            }//ifm=1

                        }
                    }).setNegativeButton("取消", null).create().show();


        }

    }//lei

